package Lobby;

import jHaggis.jHaggis;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * Fenster um ein Spiel zu erstellen!
 * 
 * @author Christopher Vogel
 */
public class jCreateGame extends jHaggis {
	// Formular: Spielename, Anzahl Spieler, Punkte!

	private JLabel lbl_GameName;
	protected JTextField txtF_GameName;
	protected JScrollPane scrollList;
	private JLabel lbl_points;
	protected JList<Integer> listbox;
	private JLabel lbl_amountPlayers;
	protected JButton btn_230pts;
	protected JButton btn_350pts;
	private mLobby model;
	protected ButtonGroup c;
	protected JRadioButton a;
	protected JRadioButton b;
	static jCreateGame singleton;
	protected JButton btnCreate;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					if (singleton == null) {
						jCreateGame frame = new jCreateGame(null);
						// this.singleton = frame();
						frame.setVisible(true);
					} else {
						System.out
								.println("Singleton! May only run one instance per time!");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws InterruptedException
	 */
	public jCreateGame(mLobby model) {
		super("Create a new game!");
		this.model = model;

		this.content.setLayout(new GridBagLayout());
		this.setSize(300, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);

		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 5, 5);
		// JLabel command
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;

		gbc.gridy = 0;
		gbc.gridx = 0;
		lbl_GameName = new JLabel("Game Room: ");
		lbl_GameName.setForeground(Color.white);
		add(lbl_GameName, gbc);

		gbc.gridy = 0;
		gbc.gridx = 1;
		// txtF_GameName
		txtF_GameName = new JTextField(10);
		add(txtF_GameName, gbc);
		gbc.gridy = 1;
		gbc.gridx = 0;

		lbl_amountPlayers = new JLabel("Anzahl Spieler: ");
		lbl_amountPlayers.setForeground(Color.white);
		add(lbl_amountPlayers, gbc);

		gbc.gridy = 1;
		gbc.gridx = 1;

		c = new ButtonGroup();
		a = new JRadioButton("2");
		b = new JRadioButton("3");

		a.setSelected(true);

		c.add(a);
		c.add(b);
		a.setOpaque(false);
		b.setOpaque(false);
		a.setForeground(Color.white);
		b.setForeground(Color.white);

		// txtF_GameName = new JTextField(10);
		add(a, gbc);

		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridy = 1;
		gbc.gridx = 1;
		add(b, gbc);

		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc.gridy = 2;
		gbc.gridx = 0;

		lbl_points = new JLabel("Punkte: ");
		lbl_points.setForeground(Color.white);
		add(lbl_points, gbc);

		gbc.gridy = 2;
		gbc.gridx = 1;

		// Only INTEGERS allowed for points

		DefaultListModel<Integer> z = new DefaultListModel<Integer>();
		int points = 350;
		while (points >= 230) {
			z.addElement(points);
			points -= 10;
		}
		listbox = new JList<Integer>(z);
		listbox.setVisibleRowCount(1);

		scrollList = new JScrollPane(listbox);
		listbox.setSelectedValue(z.lastElement(), true);

		add(scrollList, gbc);
		gbc.gridy = 3;
		gbc.gridx = 1;
		btn_230pts = new JButton("230");
		add(btn_230pts, gbc);

		gbc.anchor = GridBagConstraints.FIRST_LINE_END;
		gbc.gridy = 3;
		gbc.gridx = 1;
		btn_350pts = new JButton("350");
		add(btn_350pts, gbc);

		gbc.gridy = 4;
		gbc.gridx = 1;
		btnCreate = new JButton("Spiel erstellen");

		add(btnCreate, gbc);

		btn_230pts.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				listbox.setSelectedValue(z.lastElement(), true);
			}

		});
		btn_350pts.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				listbox.setSelectedValue(z.firstElement(), true);
			}

		});

	}


	public String getName() {
		return txtF_GameName.getText();
	}
}
